﻿using AutomatedTestDataManagement.Models;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web.Mvc;

namespace AutomatedTestDataManagement.Controllers
{
    public class TableMasterController : Controller
    {
        private ApplicationTestDataEntities db = new ApplicationTestDataEntities();

        //
        // GET: /TableMaster/

        private SelectList tableKinds;

        private SelectList loadTableKinds(string selected = "")
        {
            tableKinds = new SelectList(
                new[]
                {
                    new { text = "DATA", value = "DATA" },
                    new { text = "LINK", value = "LINK" },
                    new { text = "META", value = "META" },
                    new { text = "NAVI", value = "NAVI" }
                }, "value", "text", selected
             ); // these are static values, so it's safe to hardcode them
            
            return tableKinds;
        }
        
        public ActionResult Index()
        {
            return View(db.TableMasters.OrderBy(tm => tm.TableKind).ToList());
        }

        //
        // GET: /TableMaster/Details/5

        public ActionResult Details(int id = 0)
        {
            TableMaster tablemaster = db.TableMasters.Find(id);
            if (tablemaster == null)
            {
                return HttpNotFound();
            }
            return View(tablemaster);
        }

        //
        // GET: /TableMaster/Create

        public ActionResult Create(string id = "")
        {
            ViewBag.TableKind = loadTableKinds(id);
            return View();
        }

        //
        // POST: /TableMaster/Create

        [HttpPost]
        public ActionResult Create(TableMaster tablemaster)
        {
            if (ModelState.IsValid)
            {
                db.TableMasters.Add(tablemaster);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(tablemaster);
        }

        //
        // GET: /TableMaster/Edit/5

        public ActionResult Edit(int id = 0)
        {
            TableMaster tablemaster = db.TableMasters.Find(id);
            if (tablemaster == null)
            {
                return HttpNotFound();
            }
            ViewBag.TableKind = loadTableKinds(tablemaster.TableKind);

            return View(tablemaster);
        }

        //
        // POST: /TableMaster/Edit/5

        [HttpPost]
        public ActionResult Edit(TableMaster tablemaster)
        {
            if (ModelState.IsValid)
            {
                db.Entry(tablemaster).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.TableKind = loadTableKinds(tablemaster.TableKind);
            return View(tablemaster);
        }

        //
        // GET: /TableMaster/Delete/5

        public ActionResult Delete(int id = 0)
        {
            TableMaster tablemaster = db.TableMasters.Find(id);
            if (tablemaster == null)
            {
                return HttpNotFound();
            }
            if (tablemaster.TableKind != "DATA")
            {
                ModelState.AddModelError("", "Only DATA tables can be deleted");
            }
            if (tablemaster.Data.Count > 0)
            {
                ModelState.AddModelError("", "This table contains data. It can only be deleted if it is empty.");
            }
            return View(tablemaster);
        }

        //
        // POST: /TableMaster/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            TableMaster tablemaster = db.TableMasters.Find(id);
            db.TableMasters.Remove(tablemaster);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}